package net.opentsdb.tools;

import net.opentsdb.utils.Config;
import org.jboss.netty.logging.InternalLoggerFactory;
import org.jboss.netty.logging.Slf4JLoggerFactory;

import java.util.Map;

/**
 * @author Hujf
 * @title: CliOptions
 * @date 2021-09-08 9:25
 * @description: 处理命令行参数
 */
public final class CliOptions {
    static {
        InternalLoggerFactory.setDefaultFactory(new Slf4JLoggerFactory());
    }

    static void addCommon(final ArgP argp) {
        argp.addOption("--table", "TABLE",
                "Name of the HBase table where to store the time series"
                        + " (default: tsdb).");
        argp.addOption("--uidtable", "TABLE",
                "Name of the HBase table to use for Unique IDs"
                        + " (default: tsdb-uid).");
        argp.addOption("--zkquorum", "SPEC",
                "Specification of the ZooKeeper quorum to use"
                        + " (default: localhost).");
        argp.addOption("--zkbasedir", "PATH",
                "Path under which is the znode for the -ROOT- region"
                        + " (default: /hbase).");
        argp.addOption("--config", "PATH",
                "Path to a configuration file"
                        + " (default: Searches for file see docs).");
    }

    public static void addAutoMetricFlag(ArgP argp) {
        argp.addOption("--auto-metric", "Automatically add metrics to tsdb as they"
                + " are inserted.  Warning: this may cause unexpected"
                + " metrics to be tracked");
    }

    public static String[] parse(ArgP argp, String[] args) {
        try {
            args = argp.parse(args);
        } catch (IllegalArgumentException e) {
            System.err.println("Invalid usage.  " + e.getMessage());
            System.exit(2);
        }
        //free
        honorVerboseFlag(argp);
        return args;
    }

    private static void honorVerboseFlag(final ArgP argp) {
    }

    public static Config getConfig(final ArgP argp) {
        final Config config;
        final String config_file = argp.get("--config","");
        if(config_file.isEmpty()){
            config = new Config(true);
        }else{
            config = new Config(config_file);
        }
        // load CLI overloads
        overloadConfig(argp, config);
        // the auto metric is recorded to a class boolean flag since it's used so
        // often. We have to set it manually after overriding.
        config.setAutoMetric(config.getBoolean("tsd.core.auto_create_metrics"));
        return config;
    }

    private static void overloadConfig(ArgP argp, Config config) {
        // loop and switch so we can map cli options to tsdb options
        for (Map.Entry<String, String> entry : argp.getParsed().entrySet()) {
            // map the overrides
            if (entry.getKey().toLowerCase().equals("--auto-metric")) {
                config.overrideConfig("tsd.core.auto_create_metrics", "true");
            } else if (entry.getKey().toLowerCase().equals("--disable-ui")) {
                config.overrideConfig("tsd.core.enable_ui", "false");
            } else if (entry.getKey().toLowerCase().equals("--disable-api")) {
                config.overrideConfig("tsd.core.enable_api", "false");
            } else if (entry.getKey().toLowerCase().equals("--table")) {
                config.overrideConfig("tsd.storage.hbase.data_table", entry.getValue());
            } else if (entry.getKey().toLowerCase().equals("--uidtable")) {
                config.overrideConfig("tsd.storage.hbase.uid_table", entry.getValue());
            } else if (entry.getKey().toLowerCase().equals("--zkquorum")) {
                config.overrideConfig("tsd.storage.hbase.zk_quorum",
                        entry.getValue());
            } else if (entry.getKey().toLowerCase().equals("--zkbasedir")) {
                config.overrideConfig("tsd.storage.hbase.zk_basedir",
                        entry.getValue());
            } else if (entry.getKey().toLowerCase().equals("--port")) {
                config.overrideConfig("tsd.network.port", entry.getValue());
            } else if (entry.getKey().toLowerCase().equals("--staticroot")) {
                config.overrideConfig("tsd.http.staticroot", entry.getValue());
            } else if (entry.getKey().toLowerCase().equals("--cachedir")) {
                config.overrideConfig("tsd.http.cachedir", entry.getValue());
            } else if (entry.getKey().toLowerCase().equals("--flush-interval")) {
                config.overrideConfig("tsd.core.flushinterval", entry.getValue());
            } else if (entry.getKey().toLowerCase().equals("--backlog")) {
                config.overrideConfig("tsd.network.backlog", entry.getValue());
            } else if (entry.getKey().toLowerCase().equals("--read-only")) {
                config.overrideConfig("tsd.mode", "ro");
            } else if (entry.getKey().toLowerCase().equals("--bind")) {
                config.overrideConfig("tsd.network.bind", entry.getValue());
            } else if (entry.getKey().toLowerCase().equals("--async-io")) {
                config.overrideConfig("tsd.network.async_io", entry.getValue());
            } else if (entry.getKey().toLowerCase().equals("--worker-threads")) {
                config.overrideConfig("tsd.network.worker_threads", entry.getValue());
            } else if(entry.getKey().toLowerCase().equals("--use-otsdb-ts")) {
                config.overrideConfig("tsd.storage.use_otsdb_timestamp", "true");
            } else if (entry.getKey().toLowerCase().equals("--dtc-ts")) {
                config.overrideConfig("tsd.storage.get_date_tiered_compaction_start", entry.getValue());
            }
        }
    }
}
